﻿using System.Web;
using JSC.Portal.Entities;
using System.Web.Mvc;
using JSC.Core;

namespace JSC.Portal.Web.Helper
{
    public static class UrlHelperExtension
    {
        public static string Asset(this UrlHelper helper, string assetName)
        {
            return helper.Content("~/asset.axd") + "?name={0}".FormatWith(assetName);
        }

        //public static string Image(this UrlHelper helper, string fileName)
        //{
        //    return helper.Content("~/Assets/Images/{0}".FormatWith(fileName));
        //}

        public static string GetHostUrl()
        {
            string securePort = HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"];
            string protocol = securePort == null || securePort == "0" ? "http" : "https";
            string serverPort = HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
            string port = serverPort == "80" ? string.Empty : ":" + serverPort;
            string serverName = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
            return string.Format("{0}://{1}{2}", protocol, serverName, port);
        }

        public static string GetHostUrl(this UrlHelper helper)
        {
            string securePort = helper.RequestContext.HttpContext.Request.ServerVariables["SERVER_PORT_SECURE"];
            string protocol = securePort == null || securePort == "0" ? "http" : "https";
            string serverPort = helper.RequestContext.HttpContext.Request.ServerVariables["SERVER_PORT"];
            string port = serverPort == "80" ? string.Empty : ":" + serverPort;
            string serverName = helper.RequestContext.HttpContext.Request.ServerVariables["SERVER_NAME"];
            return string.Format("{0}://{1}{2}", protocol, serverName, port);
        }

        public static string EnsureTilde(string url)
        {
            return url.StartsWith("~/") ? url : "~/" + url;
        }

        public static string EnsureTilde(this UrlHelper helper, string url)
        {
            return EnsureTilde(url);    
        }

        public static string Combine(params string[] urls)
        {
            string result = string.Empty;
            foreach (string url in urls)
            {
                if (!string.IsNullOrEmpty(result))
                    result = EnsureTrailingSlash(result);
                result += url;
            }
            return result;
        }

        public static string Combine(this UrlHelper helper, params string[] urls)
        {
            return Combine(urls);
        }

        public static string EnsureTrailingSlash(string stringThatNeedsTrailingSlash)
        {
            if (!stringThatNeedsTrailingSlash.EndsWith("/"))
            {
                return stringThatNeedsTrailingSlash + "/";
            }
            else
            {
                return stringThatNeedsTrailingSlash;
            }
        }

        public static string EnsureTrailingSlash(this UrlHelper helper, string stringThatNeedsTrailingSlash)
        {
            return EnsureTrailingSlash(stringThatNeedsTrailingSlash);
        }
    }
    ///// <summary>
    ///// The UrlHelper class contains methods for url creation and manipulation.
    ///// </summary>
    //public class UrlHelper
    //{
    //    private UrlHelper() {}

    //    /// <summary>
    //    /// GetApplicationPath returns the base application path and ensures that it allways ends with a "/".
    //    /// </summary>
    //    /// <returns></returns>
    //    public static string GetApplicationPath()
    //    {
    //        return EnsureTrailingSlash(HttpContext.Current.Request.ApplicationPath);
    //    }

    //    /// <summary>
    //    /// Returns a formatted url for a given section (/{ApplicationPath}/{Section.Id}/section.aspx).
    //    /// </summary>
    //    /// <param name="section"></param>
    //    /// <returns></returns>
    //    public static string GetUrlFromSection(Section section)
    //    {
    //        return GetApplicationPath() + section.Id.ToString() + "/section.aspx";
    //    }

    //    /// <summary>
    //    /// Returns a formatted url for a given section (http://{hostname}/{ApplicationPath}/{Section.Id}/section.aspx).
    //    /// </summary>
    //    /// <param name="section"></param>
    //    /// <returns></returns>
    //    public static string GetFullUrlFromSection(Section section)
    //    {
    //        return GetHostUrl() + GetUrlFromSection(section);
    //    }

    //    /// <summary>
    //    /// Returns a formatted url for a rss feed for a given section 
    //    /// (http://{hostname}/{ApplicationPath}/{Section.Id}/feed.aspx).
    //    /// </summary>
    //    /// <param name="section"></param>
    //    /// <returns></returns>
    //    public static string GetRssUrlFromSection(Section section)
    //    {
    //        return GetHostUrl() + GetApplicationPath() + section.Id.ToString() + "/feed.aspx";
    //    }

    //    /// <summary>
    //    /// 
    //    /// </summary>
    //    /// <param name="pathInfo"></param>
    //    /// <returns></returns>
    //    public static string[] GetParamsFromPathInfo(string pathInfo)
    //    {
    //        if (pathInfo.Length > 0)
    //        {
    //            if (pathInfo.EndsWith("/"))
    //            {
    //                pathInfo = pathInfo.Substring(0, pathInfo.Length - 1);
    //            }
    //            pathInfo = pathInfo.Substring(1, pathInfo.Length - 1);
    //            return pathInfo.Split(new char[] { '/' });
    //        }
    //        else
    //        {
    //            return null;
    //        }
    //    }

    //    public static string GetHostUrl()
    //    {
    //        //return "http://test";
    //        string securePort = HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"];
    //        string protocol = securePort == null || securePort == "0" ? "http" : "https";
    //        string serverPort = HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
    //        string port = serverPort == "80" ? string.Empty : ":" + serverPort;
    //        string serverName = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
    //        return string.Format("{0}://{1}{2}", protocol, serverName, port);
    //    }

    //    public static string GetLanguage()
    //    {
    //        string siteAddress = GetHostUrl();
    //        string url = HttpContext.Current.Request.Url.ToString();
    //        return url.Substring(siteAddress.Length + 1, 5);
    //    }

    //    /// <summary>
    //    /// Ensure that the given string has a trailing slash.
    //    /// </summary>
    //    /// <param name="stringThatNeedsTrailingSlash"></param>
    //    /// <returns></returns>
    //    public static string EnsureTrailingSlash(string stringThatNeedsTrailingSlash)
    //    {
    //        if (!stringThatNeedsTrailingSlash.EndsWith("/"))
    //        {
    //            return stringThatNeedsTrailingSlash + "/";
    //        }
    //        else
    //        {
    //            return stringThatNeedsTrailingSlash;
    //        }
    //    }

    //    public static string EnsureTilde(string url)
    //    {
    //        //VirtualPathUtility.
    //        return url.StartsWith("~/") ? url : "~/" + url;
    //    }

    //    public static string Combine(params string[] urls)
    //    {
    //        string result = string.Empty;
    //        foreach (string url in urls)
    //        {
    //            if (!string.IsNullOrEmpty(result))
    //                result = EnsureTrailingSlash(result);
    //            result += url;
    //        }
    //        return result;
    //    }
    //}
}
